define(function () {

    var self, fwslItem;


    function dateFmt(v){
        return v==undefined?"":v.replace(" 00:00:00","")
    }

    app.newModule({
       init:function () {
           self = this;
           self._super({
               _tag: 'fwsliInfo',
               // 最先执行
               _readyBefore: function () {
                   fwslItem = app.cloneJSON(app.pageParameters['fwslinfo_page']);//获取传的参数
                   fwslItem.orderDetail = {
                       MMRA004: ''
                   };
                   fwslItem._WXB = [];
                   fwslItem._SMFWDW = '';//上门服务部门
                   fwslItem.MMRD024 = dateFmt(fwslItem.MMRD024) || dateFmt(fwslItem.MMRD004);
                   fwslItem.MMRD025 = fwslItem.MMRD025 || fwslItem.MMRD005;
                   fwslItem.imgs = [];
                   if (fwslItem.MMRD015) {
                       var imgs = fwslItem.MMRD015.split(",");
                       var prefix=app.getImgUrl();
                       imgs.forEach(function (img, index) {
                           img = (prefix+img);
                           fwslItem.imgs.push({
                               src:img
                           });
                       });
                   }
               },
           }, win);

       },
        _ready:function () {
            new Vue({
                el: app.getByid(doc, 'content'),
                data: {
                    item: app.extend(fwslItem, app.userInfos.sysUser) //合并对象
                },
                methods: {
                    preview:function (index) {
                        app.openPage({
                            src:"base/previewImages.html",
                            args: {
                                imgs:fwslItem.imgs,
                                showIndex:index
                            }
                        })

                    }
                }
            });
            self.getDetail();

            new Vue({
                el: app.getByid(doc, 'actions'),
                data: {
                    item: fwslItem
                },
                methods: {
                    closeCase: function () { // 结案
                        if (fwslItem.MMRD023 == '0') {
                            app.confirm({
                                msg: '确定结案?',
                                ok: function () {
                                    self.closeCase();
                                }
                            });
                        }else{
                            app.toast("需要上门客户不可直接结案");
                        }
                    },
                    save: function () { // 保存
                        app.confirm({
                            msg: '确定保存?',
                            ok: function () {
                                self.fwSave();
                            }
                        });
                    },
                    dispatching: function () { // 派工
                        if (fwslItem.MMRD023 == '1') { // 需要上门
                            self.dispatching();
                        }
                    }
                }
            });
        },
        // 获取上门服务部
        getWxb:function(){
            app.ajax({
                xurl: "api/wx/fwsl/getWxbByUserCode",
                // xtime: '2d',
                xdata: {
                    GWAKENT: fwslItem.MMRDENT,
                    GWAK001: fwslItem.orderDetail.MMRA001,
                },
                cb: function (result) {
                    if (result.ok) {
                        var data = result.data;
                        if (data.data.length > 0) {
                            var _data = data.data;
                            for (i in _data) {
                                if (_data[i].MMRE004 == fwslItem.MMRD009 && fwslItem._SMFWDW == '') {
                                    fwslItem._SMFWDW = _data[i].ID;
                                    break;
                                }
                            }
                            fwslItem._WXB.pushArray(_data);
                        }
                    }

                }
            });
        },
        // 获取购买的设备信息
        getDetail:function(){
            app.ajax({
                xurl: "api/wx/fwsl/getOrderDetail",
                // xtime: '2d',
                xdata: {
                    MMRA002: fwslItem.MMRD002, // 设备编号
                    MMRA003: fwslItem.MMRD003 //设备序列号
                },
                cb: function (result) {
                    if (result.ok) {
                        var data = result.data;
                        if (data.data.length > 0) {
                            fwslItem.orderDetail =  data.data[0];
                            self.getWxb();
                        }


                    }

                }
            });
        },
        //保存
        fwSave:function(){
            var _xdata = {
                MMRD023: fwslItem.MMRD023,
                MMRDENT: fwslItem.MMRDENT,
                MMRDDOCNO: fwslItem.MMRDDOCNO,
                MMRDSEQ: fwslItem.MMRDSEQ
            };
            if (_xdata.MMRD023 == '0') { //上门服务--否
                _xdata.MMRD026 = fwslItem.MMRD026;
            } else {
                _xdata.MMRD024 = dateFmt(fwslItem.MMRD024);
                _xdata.MMRD025 = dateFmt(fwslItem.MMRD025);
            }

            app.ajax({
                xurl: "api/wx/fwsl/orderSave",
                loading: true,
                xdata: _xdata,
                cb: function (result) {
                    if (result.ok) {
                        app.alert({
                            msg: '服务单保存成功！', cb: function () {
                                mui.back();
                            }
                        });
                        app.pageModule('p_fwsl')._refresh();
                    }
                }
            });
        },
        //结案
        closeCase:function(){
            var _xdata = {
                MMRD026: fwslItem.MMRD026,
                MMRDENT: fwslItem.MMRDENT,
                MMRDDOCNO: fwslItem.MMRDDOCNO,
                MMRDSEQ: fwslItem.MMRDSEQ
            };

            app.ajax({
                xurl: "api/wx/fwsl/closeCase",
                loading: true,
                xdata: _xdata,
                cb: function (result) {
                    if (result.ok) {
                        app.alert({
                            msg: '服务单结案成功！', cb: function () {
                                mui.back();
                            }
                        });
                        app.pageModule('p_fwsl')._refresh();
                    }
                }
            });
        },
        //派工
        dispatching:function(){
            app.openPage({
                src: "fwpg/fwpg-info.html",
                argsKey: 'fwpg-info',
                args: {
                    items: [fwslItem],
                    backNum: 2,
                    cb: function () {
                        //刷新数据
                        app.pageModule('p_fwsl')._refresh();
                    }
                }
            });
        },
        _lazy:function () {
            this._super();
            mui('.mui-scroll-wrapper').scroll({
                deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
            });
        }
    });
    // return new fwsliInfo();

});